<template>
  <div style="padding:10px">

    <!-- 课程信息表单 -->
    <el-form ref="form" :model="course" label-width="120px">

      <el-form-item label="课程标题">
        <el-input v-model="course.title" placeholder=" 示例：无机化学。专业名称注意大小写"/>
      </el-form-item>

      <!-- 课程讲师 TODO -->
      <el-form-item label="课程讲师">
        <el-select v-model="course.teacherId" placeholder="请选择">
          <!-- 查询之后遍历显示 -->
          <el-option v-for="teacher in teachers" :key="teacher.id" :label="teacher.name" :value="teacher.id"/>
        </el-select>
      </el-form-item>

      <!-- 所属分类 TODO -->
      <el-form-item label="课程类别">
        <el-select v-model="course.subjectParentId" placeholder="请选择" @change="levelOneSubjectchanged">
          <!-- 查询之后遍历显示 -->
          <el-option
            v-for="levelOneSubject in levelOneSubjectList"
            :key="levelOneSubject.id"
            :label="levelOneSubject.title"
            :value="levelOneSubject.id"
          />
        </el-select>
        <el-select v-model="course.subjectId" placeholder="请选择">
          <!-- 查询之后遍历显示 -->
          <el-option
            v-for="levelTwoSubject in levelTwoSubjectList"
            :key="levelTwoSubject.id"
            :label="levelTwoSubject.title"
            :value="levelTwoSubject.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="总课时">
        <el-input-number :min="0" v-model="course.lessonNum" controls-position="right" placeholder="请填写课程的总课时数"/>
      </el-form-item>

      <!-- 课程简介 TODO -->
      <el-form-item label="课程简介">
        <tinymce v-model="course.description" :height="300"/>
      </el-form-item>

      <el-form-item label="课程价格">
        <el-input-number :min="0" v-model="course.price" controls-position="right" placeholder="免费课程请设置为0元"/> 元
      </el-form-item>

      <!-- 课程封面 TODO -->
      <el-form-item label="课程封面">
        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :action="uploadUrl"
          class="avatar-uploader">
          <img v-if="course.cover" :src="course.cover" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"/>
        </el-upload>
      </el-form-item>

    </el-form>

    <div style="text-align:center">
      <el-button :disabled="flag" type="primary" @click="saveAndNext">保存并下一步</el-button>
    </div>
  </div>
</template>

<script>
import teacherApi from '@/api/teacher'
import subjectApi from '@/api/subject'
import Tinymce from '@/components/Tinymce'
import courseApi from '@/api/course'
export default {
  components: {
    Tinymce
  },
  data() {
    return {
      uploadUrl: 'http://localhost:8120/admin/oss/file/upload?module=avatar',
      teachers: [],
      course: {
        price: 0,
        lessonNum: 0,
        // 以下解决表单数据不全时insert语句非空校验
        teacherId: '',
        subjectId: '',
        subjectParentId: '',
        cover: '',
        description: ''
      },
      flag: false, // 按钮是否禁用
      levelOneSubjectList: [], // 一级课程分类
      levelTwoSubjectList: []
    }
  },
  created() {
    // 查询所有讲师
    this.getTeacherList()
    // 查询课程分类嵌套集合
    this.getSubjectNestedList()
  },
  methods: {
    // 提交第一步的课程基本信息
    saveCourseInfo() {
      courseApi.saveCourseInfo(this.course).then(response => {
        this.$parent.courseId = response.data.courseId // 获取关联课程id
        // console.log(this.$parent.courseId)
        // console.log(this.course.description)
        // this的属性可以参考vueapi: https://cn.vuejs.org/v2/api/index.html
        this.$parent.active = 1
      })
    },
    // 一级课程分类的下拉选框内容改变监听
    levelOneSubjectchanged(checkedLevelOneSubjectId) {
    //   console.log(checkedLevelOneSubjectId)
    // 查询二级课程
      this.levelOneSubjectList.forEach(LevelOneSubject => {
        if (LevelOneSubject.id === checkedLevelOneSubjectId) {
          this.course.subjectId = ''
          this.levelTwoSubjectList = LevelOneSubject.children
          return
        }
      })
    },
    // 查询课程分类嵌套集合
    getSubjectNestedList() {
      subjectApi.subjectsNestedList().then(response => {
        this.levelOneSubjectList = response.data.items
      })
    },
    // 查询所有讲师
    getTeacherList() {
      teacherApi.teacherList().then(response => {
        this.teachers = response.data.items
      })
    },
    handleAvatarSuccess(res, file) {
      this.course.cover = res.data.path
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 10

      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 10MB!')
      }
      return isJPG && isLt2M
    },
    // 保存并下一步
    saveAndNext() {
      this.flag = true
      this.saveCourseInfo()
    }
  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 356px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 356px;
    height: 178px;
    display: block;
  }
</style>
